/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef UNICAST_SESSION_MONITOR_H
#define UNICAST_SESSION_MONITOR_H

#include "jitter-sim-include.h"
#include "path-detail-calculator.h"
#include "ns3/packet-reorder-calculator.h"
#include "ns3/packet-traversed-hop-calculator.h"
#include <set>

namespace ns3 {
namespace eecityu {

using namespace ns3;

/**
 * \brief Gather the information regarding a unicast traffic session between two nodes
 */
class UnicastSessionMonitor : public Object
{
public:
  static TypeId GetTypeId (void);
  UnicastSessionMonitor (void);  
  /**
   * \brief Get the number Rx records
   * \return The number of recived packets, including duplicated packets, recorded so far
   */
  uint32_t GetNArrival () const;
  /**
   * \brief Get the number of duplicated packets recorded
   * \return The number of duplicated pacekts recorded so far
   */
  uint32_t GetNDuplicated () const;
  /**
   * \brief Get the number of missing packets
   * \return The number of mssing packets. These packets may be lost or may
   * late. Nevertheless, they are expected to be recorded but are missing currently
   */
  uint32_t GetNMissing () const;
  /**
   * \brief Get the number of recordered packets
   * \return The number of reordered packets
   */
  uint32_t GetNReordered () const;
  /**
   * \return Get the total number of packets sent from the source
   */
  uint32_t GetNPacketsSent () const;
  /**
   * \return Get the total bytes sent from the source
   */
  uint64_t GetNBytesSent () const;
  /**
   * \brief Get the reordering extent of a packet
   * \param s The sequence number of a packet 
   * \return The reordering extent of the packet being queried
   *
   * \see PacketReorderCalculator::GetReorderingExtent ()
   */
  uint64_t GetReorderingExtent (uint64_t s) const;
  /**
   * \brief The the reordering late time of a packet
   * \param s The sequence number of a packet 
   * \param The reordering late time of the packet being queried
   *
   * \see PacketReorderCalculator::GetReorderingExtent ()
   */
  Time GetLateTime (uint64_t s) const;
  /**
   * \brief Get the path distance, in hops, between source and destination nodes
   * \return The path distance between the source and the destination nodes
   * currently being monitored
   */
  uint32_t GetPathDistance () const;
  /**
   * \brief Query the internal index of this unicast session monitor object
   *
   * \return The internal index of the unicast session monitor object
   */
  uint32_t GetSessionIndex () const;
  /**
   * \brief Getter for the source node
   *
   * \return The source node of the unicast traffic session
   */
  Ptr<Node> GetSource (void) const;
  /**
   * \brief Getter for the destination node
   *
   * \return The sink node of the unicast traffic session
   */
  Ptr<Node> GetDestination (void) const;
  /**
   * \brief Get the source application associated with this session monitor
   *
   * \return The traffic sourcing application of this session monitor
   */
  Ptr<Application> GetSourceApplication () const;
  /**
   * \brief Get the sink/destination application associated with this session monitor
   *
   * \return The traffic sinking application of this session monitor
   */
  Ptr<Application> GetSinkApplication () const;
  /**
   * \brief Query if a packet is missing
   *
   * \param s The sequence number of the packet being queried, starting from zero.
   * \return True if the packet specified by the sequence number is missing
   *
   * \see PacketReorderCalculator::IsMissing ()
   */  
  bool IsMissing (uint64_t s) const;

protected:
  virtual void DoDispose (void);

private:
  /**
   * \brief Handle the event that a packet is just sent from the source node
   *
   * This method is used as a callback and is connected to a trace source to trace
   * the packet sent event. Appropriate response to packet sent event is defined
   * by this method.
   *
   * \param p The packet just sent out from the source node
   */
  void HandlePacketSentEvent (Ptr<const Packet> p);
  /**
   * \brief Handle the event that a packet is just arrived to the destination node
   *
   * This method is used as a callback and is connected to a trace source to trace
   * the packet arrived event. Appropriate response to packet arrived event is
   * defiend by this method.
   *
   * \param p The packet just received from the destination node
   * \param addr The address of the sender of the arrived packet
   */
  void HandlePacketReceivedEvent (Ptr<const Packet> p, const Address& addr);
  /**
   * \brief Set the source application for this session monitor
   *
   * \param app The soucing application to be set to this session monitor
   */
  void SetSourceApplication (Ptr<Application> app);
  /**
   * \brief Set the sink/destination application for this session monitor
   *
   * \param app The sinking application to be set to this session monitor
   */
  void SetSinkApplication (Ptr<Application> app);

  /// Register each packet sent with this sessions so that only the registered packets can be received by the same monitor
  set<uint32_t> m_packetRegister;
  /// The sourcing application of the unicast session being monitored
  Ptr<Application> m_sourceApplication;
  /// The sinking application of the unicast session being monitored
  Ptr<Application> m_sinkApplication;
  /// An measurement object for measuring QoS, such as delay, jitter, reordering, etc
  Ptr<PacketReorderCalculator> m_reorderMeasure;
  /// AN measurement ojbect for measuring the traversed hops of received packets
  Ptr<PacketTraversedHopCalculator> m_traversedHopMeasure;
  /// To identify each unicast session monitor uniquely
  uint32_t m_index;  
public:
  /// To allocate an index to each unicast session monitor object
  static uint32_t g_indexCounter;
private:
};

} // namespace eecityu
} // namespace ns3

#endif // #ifndef UNICAST_SESSION_MONITOR_H
